<template>
  <view class="container">
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-left">
        <image @click="goBack" class="back-icon" src="/static/image/值班安排.png" mode="aspectFit"></image>
      </view>
      <view class="nav-title">协助换班</view>
      <view class="nav-right">
        <image class="nav-icon" src="/static/image/更多功能.png" mode="aspectFit"></image>
        <image class="nav-icon" src="/static/image/换班申请.png" mode="aspectFit"></image>
      </view>
    </view>

    <!-- 搜索框 -->
    <view class="search-container">
      <view class="search-box">
        <text class="search-icon">🔍</text>
        <input type="text" placeholder="搜索辅导员姓名" v-model="searchKeyword" @input="onSearch"/>
        <image v-if="searchKeyword" @click="clearSearch" class="clear-icon" src="/static/image/更多功能.png" mode="aspectFit"></image>
      </view>
    </view>

    <!-- 辅导员列表 -->
    <view class="counselor-list">
      <view v-for="(counselor, index) in filteredCounselors" :key="index" class="counselor-item">
        <view class="counselor-header">
          <image class="cert-icon" src="/static/image/值班安排.png" mode="aspectFit"></image>
          <text class="counselor-name">{{ counselor.name }}</text>
          <image @click="makeCall(counselor.phone)" class="call-icon" src="/static/image/换班申请.png" mode="aspectFit"></image>
        </view>
        <view class="counselor-info">
          <text class="info-text">{{ counselor.title }} {{ counselor.id }}</text>
          <text class="info-text">所在单位: {{ counselor.department }}</text>
        </view>
        
        <view class="duty-info">
          <view class="duty-title">下一次值班:</view>
          <text class="info-text">值班开始时间: {{ counselor.dutyStartTime }}</text>
          <text class="info-text">值班结束时间: {{ counselor.dutyEndTime }}</text>
          <text class="info-text">值班校区: {{ counselor.campus }}</text>
          <text class="info-text">值班地点: {{ counselor.location }}</text>
          <text class="info-text">值班住宿地址: {{ counselor.accommodation }}</text>
          <text class="info-text">今日值班状态: {{ counselor.status }}</text>
        </view>
        
        <view class="action-buttons">
          <button class="assist-button" @click="assistShift(counselor.id)">协助换班</button>
          <button v-if="selectedCounselorId === counselor.id" class="execute-button" @click="executeShift">执行换班</button>
        </view>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="nav-bar-bottom">
      <view class="nav-item" @click="navigateToDutyStatus">
        <image class="nav-icon-bottom" src="/static/image/值班安排.png" mode="aspectFit"></image>
        <text class="nav-text">值班情况</text>
      </view>
      <view class="nav-item active" @click="navigateToAssist">
        <image class="nav-icon-bottom" src="/static/image/换班申请.png" mode="aspectFit"></image>
        <text class="nav-text">协助换班</text>
      </view>
      <view class="nav-item" @click="navigateToMoreFunctions">
        <image class="nav-icon-bottom" src="/static/image/更多功能.png" mode="aspectFit"></image>
        <text class="nav-text">更多功能</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      selectedCounselorId: '',
      counselors: [
        {
          id: '10086',
          name: '王1',
          title: '王1辅导员',
          department: 'A学院',
          phone: '13800138001',
          dutyStartTime: '2025-02-24 18:00:00',
          dutyEndTime: '2025-02-25 08:00:00',
          campus: 'A校区',
          location: '新勇学生活动中心3层辅导员发展中心',
          accommodation: '值班住宿地址',
          status: '未开始'
        },
        {
          id: '10087',
          name: '王2',
          title: '王2辅导员',
          department: 'A学院',
          phone: '13800138002',
          dutyStartTime: '2025-02-24 18:00:00',
          dutyEndTime: '2025-02-25 08:00:00',
          campus: 'A校区',
          location: '新勇学生活动中心3层辅导员发展中心',
          accommodation: '值班住宿地址',
          status: '未开始'
        }
      ]
    }
  },
  computed: {
    filteredCounselors() {
      if (!this.searchKeyword) {
        return this.counselors;
      }
      return this.counselors.filter(counselor => 
        counselor.name.includes(this.searchKeyword)
      );
    }
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },
    onSearch() {
      // 搜索逻辑已在computed中实现
    },
    clearSearch() {
      this.searchKeyword = '';
    },
    makeCall(phone) {
      uni.makePhoneCall({
        phoneNumber: phone
      });
    },
    assistShift(counselorId) {
      // 当点击另一个辅导员的协助换班按钮时，更新选中状态
      if (this.selectedCounselorId === counselorId) {
        // 如果再次点击同一个辅导员，保持选中状态
        return;
      }
      
      // 显示确认弹窗，提示是否选择该辅导员进行换班
      uni.showModal({
        title: '选择换班',
        content: '确定选择该辅导员进行换班操作吗？',
        success: (res) => {
          if (res.confirm) {
            this.selectedCounselorId = counselorId;
          }
        }
      });
    },
    executeShift() {
      // 显示执行换班确认弹窗
      uni.showModal({
        title: '执行换班',
        content: '确定要为选中的辅导员执行换班操作吗？',
        success: (res) => {
          if (res.confirm) {
            // 执行换班逻辑
            uni.showLoading({
              title: '处理中...'
            });
            
            // 模拟换班处理
            setTimeout(() => {
              uni.hideLoading();
              uni.showToast({
                title: '换班成功',
                icon: 'success'
              });
              // 清除选中状态
              this.selectedCounselorId = '';
            }, 1500);
          }
        }
      });
    },
    navigateToDutyStatus() {
      uni.navigateTo({
        url: '/pages/admin/duty-status'
      });
    },
    navigateToAssist() {
      // 当前已经在协助换班页面，不跳转
    },
    navigateToMoreFunctions() {
      uni.navigateTo({
        url: '/pages/admin/more-functions'
      });
    }
  }
}
</script>

<style scoped>
.container {
  padding-bottom: 120rpx;
}

.nav-bar {
  display: flex;
  height: 88rpx;
  background-color: #007AFF;
  align-items: center;
  justify-content: space-between;
  padding: 0 30rpx;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.back-icon {
  width: 44rpx;
  height: 44rpx;
}

.nav-title {
  color: white;
  font-size: 36rpx;
  font-weight: bold;
}

.nav-icon {
  width: 44rpx;
  height: 44rpx;
  margin-left: 20rpx;
}

.search-container {
  padding: 20rpx 30rpx;
  margin-top: 88rpx;
  background-color: white;
}

.search-box {
  display: flex;
  align-items: center;
  background-color: #f0f0f0;
  border-radius: 40rpx;
  padding: 0 20rpx;
  height: 70rpx;
}

.search-icon {
  font-size: 32rpx;
  color: #999;
}

input {
  flex: 1;
  height: 100%;
  padding: 0 20rpx;
  font-size: 28rpx;
  background-color: transparent;
}

.clear-icon {
  width: 40rpx;
  height: 40rpx;
}

.counselor-list {
  padding: 0 30rpx;
}

.counselor-item {
  background-color: white;
  border-radius: 10rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.counselor-header {
  display: flex;
  align-items: center;
  margin-bottom: 15rpx;
}

.cert-icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 10rpx;
}

.counselor-name {
  font-size: 32rpx;
  font-weight: bold;
  flex: 1;
}

.call-icon {
  width: 40rpx;
  height: 40rpx;
}

.counselor-info {
  margin-bottom: 20rpx;
}

.duty-info {
  background-color: #f8f8f8;
  border-radius: 8rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
}

.duty-title {
  font-size: 28rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.info-text {
  display: block;
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.info-text:last-child {
  margin-bottom: 0;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
}

.assist-button {
  background-color: #e0e0e0;
  color: #333;
  border: none;
  padding: 10rpx 40rpx;
  border-radius: 40rpx;
  font-size: 28rpx;
}

.execute-button {
  background-color: #007AFF;
  color: white;
  border: none;
  padding: 10rpx 40rpx;
  border-radius: 40rpx;
  font-size: 28rpx;
  margin-left: 20rpx;
}

.nav-bar-bottom {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120rpx;
  background-color: white;
  border-top: 1px solid #e0e0e0;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.nav-item.active .nav-text {
  color: #007AFF;
}

.nav-icon-bottom {
  width: 60rpx;
  height: 60rpx;
}

.nav-text {
  font-size: 24rpx;
  color: #888;
  margin-top: 8rpx;
}
</style>